import {layoutRouter} from '@/router/index'

const router = {
    state: {
        dynamic: {}, // 动态路由集
        routerChange: false
    },

    mutations: {
        UPDATE_ROUTERS: (state, list) => {
            // 标记路由已动态更改
            state.routerChange = true
        },
        ADD_DYNAMIC: (state, list) => {
            // 将动态路由赋值给dynamic对象，方便组件中获取数据
            state.dynamic.layoutRouter = layoutRouter
        }
    },

    actions: {
        generateRouters({commit}, user) {
            // 动态添加路由
            return new Promise((resolve, reject) => {
                commit('ADD_DYNAMIC')
                let routeList = [] // 即将动态添加的路由集
                // if (user.getters.userRole === 1) {
                //     routeList = [...layoutRouter]
                // } else if (user.getters.userRole === 2 || user.getters.userRole === 3) {
                //     routeList = [...spManagerRouter]
                // }
                resolve(routeList)
            })
        },
        generateAllRouters({commit}, user) {
            // 动态添加所有的路由
            return new Promise((resolve, reject) => {
                commit('ADD_DYNAMIC')
                let routeList = [] // 即将动态添加的路由集
                routeList = [...layoutRouter]
                resolve(routeList)
            })
        },
        updateRouters({commit}) {
            commit('UPDATE_ROUTERS')
        }
    }
}

export default router
